import re

base_filter_start_symbol_re = re.compile("^[`‘’'。.·？?！!，,、/\\\；;：:“ ”\"─\-…—)_]+")

base_filter_end_symbol_re = re.compile("[`‘’'。.·？?！!，,、/\\\；;：:“ ”\"─\-…—(_]+$")

symbol_str = " '\t\n\r·.\s♀♂↑↓←→@!~#$%^&*" \
             "¤☆★△▲□■○●◎⊙◇◆◤◢◣◥" \
             "※卐" \
             "ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹ" \
             "#?。”？“💨💕🈶️/ä½╥ω╥" \
             "͡° ͜ʖ ͡° ✧◐‿◑ˊㅿˋ ┗┐ヽ ′Д、`ﾉ┌┛ " \
             "🐉🌈🐓🐐🐑🐮🐂🚜" \
             "🏊🕺💃" \
             "😂👈🏻👉🏻☝👆🏻🐵🔮🤚🖐♂️🤷🏻🤦🏻(\u200d)"
base_filter_global_symbol_re = re.compile(fr"[{symbol_str}]")

base_convert_left_bracket_re = re.compile("[【[{《〈﹤“「『〖﹄﹂｢]+")

base_convert_right_bracket_re = re.compile("[】\]}》〉﹥”」』〗﹃﹁｣]")

base_convert_zero_re = re.compile("[ＯO〇０○]+")

base_extract_bracket_re = re.compile("\([^)]*\)|（[^）]*）+")

filter_symbol_re_save_space_re = re.compile('[\\\/、,，·。.\'`"*‘’＊?~#@$☆△★▲■:XⅠⅩⅡⅣ※]+$|[\\"]+')

address_only_pcd_re = re.compile("^([^省]+?省)?([^市]+?市)?([^校院区]+?[区市县])?$")

exclude_address_only_pcd_re = re.compile(
    "[A-Za-z0-9]|社区|小区|校区|院区|片区|服务区|居民区|住宅区|生活区|商住区|厂区|安置区|集市|门市|超市|夜市|综合市|菜市|街市|农贸市场")

base_invalid_str_re = re.compile("^(#N/A)$")

only_number_re = re.compile("\\d+")
number_unit_re = re.compile("\\d+(亿|千万|百万|十万|万|千|百|十)")


def str_full_width_to_half_width(ustring):
    """全角转半角"""
    rstring = ""
    for uchar in ustring:
        inside_code = ord(uchar)
        if inside_code == 12288:  # 全角空格直接转换
            inside_code = 32
        elif 65281 <= inside_code <= 65374:  # 全角字符（除空格）根据关系转化
            inside_code -= 65248

        rstring += chr(inside_code)
    return rstring


def str_half_width_to_full_width(ustring):
    """半角转全角"""
    rstring = ""
    for uchar in ustring:
        inside_code = ord(uchar)
        if inside_code == 32:  # 半角空格直接转化
            inside_code = 12288
        elif 32 <= inside_code <= 126:  # 半角字符（除空格）根据关系转化
            inside_code += 65248

        rstring += chr(inside_code)
    return rstring


def pre_handle_str(s: str, filter_re=base_filter_global_symbol_re):
    # 全角转换为半角
    new_str = str_full_width_to_half_width(s).strip()
    # 过滤掉特殊符号
    return re.sub(filter_re, "", new_str).strip()


def pre_handle_line(line: list, filter_re=base_filter_global_symbol_re):
    '''
    全角转半角 且 过滤了特殊符号
    :param filter_re:
    :param line:
    :return:
    '''
    return [pre_handle_str(str(s), filter_re=filter_re) if s else None for s in line]


def pre_handle_str_save_space(s: str):
    return pre_handle_str(s, filter_re=filter_symbol_re_save_space_re)


def filter_symbol(original_name: str, is_need_filter_start_end: bool = False) -> str:
    """
    过滤 符号
    1: 全局过滤掉 特殊符号
    2: 过滤掉 开头的符号
    3: 过滤掉 结尾的符号
    :param is_need_filter_start_end:
    :param original_name:
    :return:
    """
    name_processed = re.sub(base_filter_global_symbol_re, " ", original_name)
    if is_need_filter_start_end:
        name_processed = re.sub(base_filter_start_symbol_re, "", name_processed)
        name_processed = re.sub(base_filter_end_symbol_re, "", name_processed)
    return name_processed


def formatter_symbol(original_name: str) -> str:
    """
    格式化 符号
    主要 将符号格式化为 ()0
    :param original_name:
    :return:
    """
    name_processed = re.sub(base_convert_left_bracket_re, "(", original_name)
    name_processed = re.sub(base_convert_right_bracket_re, ")", name_processed)
    name_processed = re.sub(base_convert_zero_re, "0", name_processed)
    return name_processed


def filter_symbol_4_word(s: str, is_need_formatter: bool = False, is_need_filter_start_end: bool = False):
    ss = str(s)
    # 全角转半角
    ss = str_full_width_to_half_width(ss)
    # 格式化符号
    if is_need_formatter:
        ss = formatter_symbol(ss)
    # 过滤特殊符号
    ss = filter_symbol(ss, is_need_filter_start_end=is_need_filter_start_end)
    return ss


def filter_symbol_4_line_with_exclude(line: list, exclude_index_list: list) -> list:
    if exclude_index_list:
        new_line = []
        for i, s in enumerate(line):
            if i in exclude_index_list:
                new_line.append(str(s))
            else:
                # 格式化成 字符串
                ss = filter_symbol_4_word(s)
                new_line.append(ss)
        return new_line
    else:
        return filter_symbol_4_line(line)


def filter_symbol_4_line(line: list) -> list:
    new_line = []
    for s in line:
        ss = filter_symbol_4_word(s)
        new_line.append(ss)
    return new_line


def is_invalid(s: str) -> bool:
    match = re.match(base_invalid_str_re, s)
    return True if match else False


def contains_invalid(line: list) -> bool:
    for s in line:
        if is_invalid(s):
            return True
    return False


def extract_number(s: str) -> int:
    if re.match(number_unit_re, s):
        return extract_number_with_unit(s)
    elif re.match(only_number_re, s):
        return extract_number_only(s)
    else:
        return 0


def extract_number_only(s: str) -> int:
    if re.match(only_number_re, s):
        match = re.search(only_number_re, s)
        group = match.group()
        return int(group)
    else:
        return 0


def extract_number_with_unit(s: str) -> int:
    if re.match(number_unit_re, s):
        if "亿" in s:
            return extract_number_only(s) * 100000000
        elif "千万" in s:
            return extract_number_only(s) * 10000000
        elif "百万" in s:
            return extract_number_only(s) * 1000000
        elif "十万" in s:
            return extract_number_only(s) * 100000
        elif "万" in s:
            return extract_number_only(s) * 10000
        elif "千" in s:
            return extract_number_only(s) * 1000
        elif "百" in s:
            return extract_number_only(s) * 100
        elif "十" in s:
            return extract_number_only(s) * 10
        else:
            return 0
    return 0


def text_plain_str(text: str) -> str:
    replace = text.replace("\n", "\t")
    return filter_no_content_str(replace)


def filter_no_content_str(text: str) -> str:
    replace = text.replace(" ", "")
    return replace.replace(",", " ")


if __name__ == "__main__":
    t = "2万+"
    plain_str = extract_number(t)
    print(plain_str)
